//
//  AppUtility.m
//  atXiaoGeBClient
//
//  Created by Linyu Niu on 2018/7/4.
//  Copyright © 2018 Linyu Niu. All rights reserved.
//

#import "AppUtility.h"

@implementation AppUtility
@synthesize user;



-(instancetype)init
{
    self = [super init];
    [self readUserFromLocal];
    return self;
}

static AppUtility * instance = nil;
static dispatch_once_t onceToken;
+(AppUtility *)shareInstance
{
    
    dispatch_once(&onceToken, ^{
        if (instance == nil) {
            instance = [[AppUtility alloc]init];
        }
    });
    
    return instance;
}
-(void)readUserFromLocal{
    NSDictionary * dic = [[NSUserDefaults standardUserDefaults] objectForKey:@"UserData"];
    
    user = [[UserModel alloc]initWithDictionary:dic error:nil];
    
}
-(void)saveUserData:(NSDictionary *)dic{
    if([dic objectForKey:@"token"]){
        [[NSUserDefaults standardUserDefaults] setObject:dic forKey:@"UserData"];
        [[NSUserDefaults standardUserDefaults] synchronize];
        [self readUserFromLocal];
    }
}
-(void)cleanUserData{
    [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"UserData"];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

-(BOOL)isLogin{
    if(user){
        return YES;
    }
    return NO;
}
@end
